<?php

namespace App\Admin\Controllers;


use App\Group;
use App\ReFund;
use App\Http\Controllers\Controller;
use App\User;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use App\Admin\Extensions\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ReFundController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('退款审核管理')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('退款审核管理')
            ->description('详情')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('退款审核管理')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('新增')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return With
     */
    protected function grid()
    {
        $grid = new Grid(new ReFund);
        $grid->id('ID');
        $grid->user()->name('退款用户');
        $grid->amount('退款金额');
        $grid->refund_desc('退款原由');
        $grid->state('状态')->using(['0' => '未通过', '1' => '已通过', '2' => '已撤销']);
        $grid->created_at('新增时间');
        $grid->updated_at('编辑时间');
        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
            $actions->state('审核');
            $actions->disableView();
            $actions->disableEdit();
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ReFund::findOrFail($id));

        $show->id('ID');
        $show->created_at('新增时间');
        $show->updated_at('编辑时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ReFund);

        $form->display('ID');
//        $form->text('state');
        $form->display('Created at');
        $form->display('Updated at');

        return $form;
    }

    public function update($id)
    {
        if (request()->get('action') == 'state') {

            $status = false;
            $message = '修改失败';
            $model = ReFund::findOrFail($id);
            $userDeposit = User::findOrFail($model['user_id']);
            $deposit = $userDeposit['deposit'] - $model['deposit'];
            $slug = $model['user_id'] . '_' . $model['chest_id'] . '_' . $model['grip_id'];
            if (!$model->isClosed()) {
                $model->update(['state' => 1]);
                $group = Group::where(['slug' => $slug,])->update(['status' => '2']);
                $upDeposit = $userDeposit->update(['deposit' => $deposit]);
                $status = true;
                $message = '修改成功';
            }
            return response([
                'status' => $status,
                'message' => $message,
            ]);

        } else {
            return $this->form()->update($id);
        }
    }
}
